package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.ProductMapper;
import com.zhentao.mapper.TOrderMapper;
import com.zhentao.pojo.Product;

import com.zhentao.pojo.TOrder;
import com.zhentao.pojo.User;
import com.zhentao.service.UserService;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("us")
@RestController
public class userController {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private TOrderMapper orderMapper;
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public Result login(@RequestBody User user){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User user1=userService.getOne(wrapper);
        return user1!=null?Result.OK(user1):Result.ERROR();
    }
    @RequestMapping("/zhuce")
    public Result zhuce(@RequestBody User user){
        boolean save = userService.save(user);
        return save?Result.OK():Result.ERROR();
    }
    /*全部商品*/
    @RequestMapping("list")
    public Result list(){
        QueryWrapper<Product> objectQueryWrapper = new QueryWrapper<>();
        List<Product> products = productMapper.selectList(objectQueryWrapper);
        return products!=null?Result.OK(products):Result.ERROR();
    }

    /*查看详情*/
    @RequestMapping("list2")
    public Result list2(Integer id){
        Product product = productMapper.selectById(id);
        return product!=null?Result.OK(product):Result.ERROR();
    }

    /*我的订单*/
    @RequestMapping("listOrder")
    public Result listOrder(Integer id){
        TOrder order1 = orderMapper.selectById(id);
        return order1!=null?Result.OK(order1):Result.ERROR();
    }
}
